<template>
	<view>
		<view class="text-center" style="padding-top: 130rpx;padding-bottom: 70rpx;font-size: 55rpx;">注册账号</view>

		<view class="px-2">
			<view class="mb-2">
				<input type="number" maxlength="11" v-model="phone" placeholder="手机号" class="border-bottom p-2" />
			</view>
			<view class="mb-2">
				<input type="text" v-model="email" placeholder="请输入邮箱, 用于找回密码" class="border-bottom p-2" />
			</view>
			<view class="mb-2">
				<input type="password" maxlength="16" v-model="password" placeholder="请输入密码"
					class="border-bottom p-2" />
			</view>
			<view class="mb-2">
				<input type="password" maxlength="16" v-model="confirmPassword" placeholder="请再次输入密码"
					class="border-bottom p-2" />
			</view>
		</view>

		<view class="py-2 px-3">
			<button class="text-white" style="border-radius: 50rpx;border: 0;" type="primary" :disabled="disabled"
				:class="disabled ? 'bg-main-disabled':'bg-main'" @click="submit">注册</button>
		</view>

		<view class="flex align-center justify-center pt-2 pb-4">
			<view class="text-primary font-sm" @click="redirectTo('login')">已有账号，去登录</view>
		</view>

		<!-- <view class="pt-4 mt-4 flex align-center justify-center text-muted">
			注册即代表同意<text class="text-primary" @click="navigateTo('UserAgreement')">《社区用户协议》</text>
		</view> -->

	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		useStore
	} from 'vuex';
	import $U from '@/common/util.js';

	const store = useStore();

	const phone = ref('');
	const email = ref('');
	const password = ref('');
	const confirmPassword = ref('');

	/**
	 * 计算属性
	 * computed
	 */
	const disabled = computed(() => {
		if (phone.value === '' || email.value === '' || password.value === '' || confirmPassword.value ===
			'') {
			return true
		}
		return false;
	});

	/**
	 * 事件
	 * methods
	 */
	const changeStatus = () => {
		status.value = !status.value;
	}

	// 跳转页面 - 可返回
	const navigateTo = (path) => {
		$U.navigateTo(path);
	}

	// 重定向页面 - 不可返回
	const redirectTo = (path) => {
		$U.redirectTo(path);
	}

	// 提示
	const toast = (title) => {
		uni.showToast({
			title,
			icon: "none"
		});
	}

	// 验证
	const check = () => {
		if ($U.checkPhone(phone.value) && $U.checkEmail(email.value, {
				required: true
			}) && $U.checkPassword(password.value)) {
			if (!confirmPassword.value) {
				toast('确认密码不能为空');
				return false;
			} else if (confirmPassword.value.length < 6) {
				toast('确认密码不能少于6位');
				return false
			} else if (password.value !== confirmPassword.value) {
				toast('两次密码不一致');
				return false;
			}

			return true;
		}
		return false;
	}

	// 注册
	const submit = () => {
		if (check()) {
			store.dispatch('registry', {
				phone: phone.value,
				email: email.value,
				password: password.value
			});
		}
	}
</script>

<style lang="less">

</style>
